<%--
  Created by IntelliJ IDEA.
  User: dd
  Date: 2023/5/6
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>做题记录</title>
    <script type="application/javascript" src="${pageContext.request.contextPath}/static/js/jquery-3.6.1.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/css/layui.css" >
    <script src="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/layui.js"></script>
    <script type="text/html" id="barDemo">
<%--        <a lay-event="edit" class="layui-btn layui-btn-xs">编辑</a>--%>
        <a lay-event="detail" class="layui-btn layui-btn-normal layui-btn-xs">详情</a>
<%--        <a lay-event="del" class="layui-btn layui-btn-danger layui-btn-xs">删除</a>--%>
    </script>
</head>
<body>
<table id="demo" class="demo-carousel" lay-filter="test" ></table>

<form class="layui-form" action="" id="updateTestForm" style="display: none" lay-filter="updateTestFormFilter">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" required lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话号码</label>
        <div class="layui-input-block">
            <input type="text" name="tel" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邀请码</label>
        <div class="layui-input-block">
            <input type="text" name="yqcode" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-block">
            <input type="date" name="createtime" required lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formUpdateTest">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(['table','form'], function(){
        let table = layui.table;
        let form = layui.form;
        table.render({
            elem: '#demo'
            ,url: '${pageContext.request.contextPath}/TSdsRecordServlet?method=query' //数据接口
            ,page: true //开启分页
            ,limit:10
            // ,toolbar:'#toolbarDemo'
            // ,autoSort:false
            ,limits:[5,10,15,20,25,30,100,200]
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left',id: 'test'},
                {field: 'id',  width:'5%',title: 'ID', sort: true, fixed: 'left' }
                ,{field: 'username', title: '用户名'}
                ,{field: 'tel', title: '电话号码'}
                ,{field: 'yqcode', title: '邀请码'}
                ,{field: 'createtime',title: '创建时间'}
                ,{field: 'right',width:'20%', title: '操作',toolbar:'#barDemo'}
            ]]
        });

        table.on('tool(test)', function(obj){
            var data = obj.data //获得当前行数据
            var id=data.id;
            if(obj.event === 'detail'){
                layer.open({
                    type: 1, // page 层类型
                    area: ['500px', '500px'],
                    title: '做题详情',
                    shade: 0.6, // 遮罩透明度
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content:'<table id="demo1" class="demo-carousel" lay-filter="test" ></table>',
                    success:function () {
                        layui.use(['table','form'], function(){
                            let table = layui.table;
                            let form = layui.form;
                            table.render({
                                elem: '#demo1'
                                ,url: '${pageContext.request.contextPath}/TSdsRecordDetailServlet?method=query&id='+id //数据接口
                                // ,page: true //开启分页
                                ,limit:10
                                // ,toolbar:'#toolbarDemo'
                                // ,autoSort:false
                                ,limits:[5,10,15,20,25,30,100,200]
                                ,cols: [[ //表头
                                    // {type: 'checkbox', fixed: 'left',id: 'test'},
                                    {field: 'id',  width:'15%',title: 'ID', sort: true, fixed: 'left' }
                                    // ,{field: 'recordid', title: '用户id'}
                                    ,{field: 'sdssub', title: '题目'}
                                    ,{field: 'sdsanswer',width:'15%', title: '答案',templet: function(d){
                                            let s=d.sdsanswer;
                                            if (+s===1){
                                                return "很少"
                                            }else if (+s===2) {
                                                return "有时"
                                            }else if (+s===3) {
                                                return "经常"
                                            }else if (+s===4) {
                                                return "持续"
                                            }
                                            //得到当前行数据，并拼接成自定义模板
                                            // return 'ID：'+ d.id +'，标题：<span style="color: #c00;">'+ d.title +'</span>'
                                        }}
                                ]]
                            });
                        })
                    }
                });
            } else if(obj.event === 'del'){
                layer.confirm('是否执行？', function(index){
                    console.log(obj);
                    $.get(
                        "${pageContext.request.contextPath}/TSdsRecordServlet?method=del"
                        , data
                        ,function (d){
                            if (+d.code===0){
                                table.reload('demo');
                            }
                        }
                        ,"json"
                    )
                    layer.close(index);
                    return false;
//向服务端发送删除指令
                });

            }else if(obj.event === 'edit'){
                layer.open({
                    title: '修改记录',
                    offset:'50px',
                    type: 1,
                    content: $('#updateTestForm'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    area: ['500px', '300px']
                });
//表单数据的填充
                form.val('updateTestFormFilter', data);
//弹出对话框，添加用户的form表单
//监听form提交事件
                form.on('submit(formUpdateTest)', function(index){
//ajax方式提交表单数据，将数据保存至数据库中
                    $.get(
                        "${pageContext.request.contextPath}/TSdsRecordServlet?method=upd"
                        , index.field
                        ,function (d){
                            if (+d.code===0){
                                table.reload('test');
                            }
                        }
                        ,"json"
                    )
                    layer.close(index);
                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });

            }
        })
    })


</script>
</body>
</html>
